package com.hdit.guimei.web;

import com.hdit.guimei.domain.Admin;
import com.hdit.guimei.service.AdminService;
import com.hdit.guimei.util.Md5Util;
import com.hdit.guimei.util.RandomValidateCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import sun.misc.BASE64Encoder;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.IOException;


/**
 * @基本功能:
 * @ClassName: AdminServlet
 * @Description: TODO
 * @Author: 李嘉明
 * @Date: 2021/1/18 14:33
 * @Version 1.0
 */
@Controller
@RequestMapping(value = "/admin")
public class AdminController {
    @Autowired
    private AdminService adminService;


    /**
     * 获取生成验证码显示到 UI 界面
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    @RequestMapping(value="/checkCode")
    public void checkCode(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //设置相应类型,告诉浏览器输出的内容为图片
        response.setContentType("image/jpeg");
        //设置响应头信息，告诉浏览器不要缓存此内容
        response.setHeader("pragma", "no-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expire", 0);
        RandomValidateCode randomValidateCode = new RandomValidateCode();
        try {
            randomValidateCode.getRandcode(request, response);//输出图片方法
        } catch (Exception e) {
            e.printStackTrace();
        }
    }



    /**
    * @Method: adminRegister
    * @函数(方法)功能: 管理员注册
    * @Author: 李嘉明
    * @params:  [admin]
    * @Return: int
    * @Exception:
    * @Date: 2021/1/19 15:49
    */
    @RequestMapping(value = "/adminRegister")
    @ResponseBody
    public int adminRegister(Admin admin) {
        System.out.println(admin);
        admin.setAdmin_pwd(Md5Util.md5(admin.getAdmin_pwd()));
        int isAdd = adminService.adminRegister(admin);
        if (isAdd == 1) {
            return 1;//注册成功
        }
        return 0;//注册失败
    }

    /**
     * @Method: adminLogin
     * @函数(方法)功能: 管理员登录
     * @Author: 李嘉明
     * @params: [admin_name, admin_pwd, session]
     * @Return: int
     * @Exception:
     * @Date: 2021/1/18 15:13
     */
    @PostMapping(value = "/adminLogin")
    @ResponseBody

    public int adminLogin( String username, String password, HttpSession session) {
        int isLogin = adminService.adminLogin(username, Md5Util.md5(password));

        System.out.println(username + password);
        //判断登录
        if (isLogin == 1) {
            session.setAttribute("admin_name", username);
            return 1;
//            return "{\"errno\": 0 , \"token\": \"dasfasdfasdfasdfasdf\"}";    //登录成功
        } else {
            if (isLogin == 0) {
                return 0;
//                return "{\"errno\": 0 , \"token\": \"dasfasdfasdfasdfasdf\"}";//登录用户不存在
            } else {
                return -1;
//                return "-111"; //登录密码不存在
            }
        }
    }
    /**
    * @Method: updateAdminPwd
    * @函数(方法)功能: 修改管理员密码
    * @Author: 李嘉明
    * @params:  [admin_name, admin_pwd, new_admin_pwd]
    * @Return: int
    * @Exception:
    * @Date: 2021/1/18 22:23
    */
    @RequestMapping(value = "/updateAdminPwdByName")
    @ResponseBody
    public int updateAdminPwdByName(String admin_name, String admin_pwd, String new_admin_pwd) {
        System.out.println(Md5Util.md5(admin_pwd));
        System.out.println(Md5Util.md5(new_admin_pwd));
        int isUpdate = adminService.updateAdminPwd(admin_name, Md5Util.md5(admin_pwd), Md5Util.md5(new_admin_pwd));
        if (isUpdate == 2) {
            return 2;//修改的用户不存在
        }
        if (isUpdate == -1) {
            return -1;//修改的密码错误
        }
        if (isUpdate == 1) {
            return 1;//修改成功
        }
        return 0;//修改失败
    }
}
